package com.kj.xuyuan.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.SelectAddressAdapter;
import com.kj.xuyuan.model.JsonBean;
import com.kj.xuyuan.util.DisplayUtil;
import com.kj.xuyuan.util.GetJsonDataUtil;
import com.kj.xuyuan.util.ScreenUtils;
import com.kj.xuyuan.widget.FullyGridLayoutManager;
import com.kj.xuyuan.widget.HorizontalSpacingItemDecoration;
import com.luck.picture.lib.decoration.GridSpacingItemDecoration;

import org.json.JSONArray;

import java.util.ArrayList;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectFriendAddressPopWindow extends PopupWindow {

    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.rv_address)
    RecyclerView rvAddress;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    private Activity mContext;
    private View view;
    private ArrayList<JsonBean> jsonBean;
    private SelectAddressAdapter adapter;
    private String selectedProvinceName;


    public SelectFriendAddressPopWindow(Activity context) {
        super(context);
        this.mContext = context;
        // 初始化页面
        init();
    }

    // 初始化布局
    private void init() {
        final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.layout_pop_select_friend_address, null);
        ButterKnife.bind(this, view);
        setFocusable(true);

        String JsonData = new GetJsonDataUtil().getJson(mContext, "province.json");//获取assets目录下的json文件数据
        jsonBean = parseData(JsonData);
        JsonBean bean = new JsonBean();
        bean.setName("全部");
        jsonBean.add(0,bean);
        rvAddress.setLayoutManager(new FullyGridLayoutManager(mContext,3));
        rvAddress.addItemDecoration(new HorizontalSpacingItemDecoration(3, DisplayUtil.dp2px(mContext,30),DisplayUtil.dp2px(mContext,30)));
        adapter = new SelectAddressAdapter();
        rvAddress.setAdapter(adapter);
        adapter.setNewData(jsonBean);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                selectedProvinceName = jsonBean.get(position).getName();
                for(int i = 0;i<jsonBean.size();i++){
                    jsonBean.get(i).setSelected(false);
                }
                jsonBean.get(position).setSelected(true);
                adapter.notifyDataSetChanged();
            }
        });

        // 导入布局
        this.setContentView(view);
        this.setClippingEnabled(false);
        setWidth(ScreenUtils.getScreenWidth(mContext));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setOutsideTouchable(true);
        final ColorDrawable dw = new ColorDrawable(0x0000000);
        setBackgroundDrawable(dw);
        setAlpha(0.7f);
    }

    public ArrayList<JsonBean> parseData(String result) {//Gson 解析
        ArrayList<JsonBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }

    @Override
    public void dismiss() {
        setAlpha(1);
        super.dismiss();
    }

    private void setAlpha(float alpha) {
        WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
        params.alpha = alpha;
        mContext.getWindow().setAttributes(params);
    }

    @OnClick({R.id.iv_close, R.id.tv_confirm})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                dismiss();
                break;
            case R.id.tv_confirm:
                if (listener != null)
                    listener.selectedProvince(selectedProvinceName);
                dismiss();
                break;
        }
    }

    private SelectProvinceListener listener;

    public void setSelectProvinceListener(SelectProvinceListener listener){
        this.listener = listener;
    }

    public interface SelectProvinceListener{
        void selectedProvince(String province);
    }

}
